﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using QLVMBAYDatacess;
using QLVMBAYDomain;

namespace QLVMBAY.Admin
{
    public partial class QLLoaiVe : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrid();
            }
        }
        private void LoadGrid()
        {
            grdLoaiVe.DataSource = LoaiVeDA.Inst.GetAll(true);
            grdLoaiVe.DataBind();
            if (grdLoaiVe.PageCount <= 1)
            {
                grdLoaiVe.PagerStyle.Visible = false;
            }
        }
        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            pnView.Visible = false;
            txtMaLV.Text = "";
            txtMaLV.ReadOnly = false;
            txtTenLV.Text = "";
            txtPhiHuy.Text="0";
            Insert = true;
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                LoaiVe LV = new LoaiVe();
                LV.MaLoaiVe = txtMaLV.Text;
                LV.TenLoaiVe = txtTenLV.Text;
                LV.PhiHuy = decimal.Parse(txtPhiHuy.Text);                
                if (Insert == true)
                {
                    LoaiVeDA.Inst.Insert(LV);
                }
                else
                {
                    LoaiVeDA.Inst.Update(LV);
                }
                LoadGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                Insert = false;
            }
        }

        protected void grdLoaiVe_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var itemLV = LoaiVeDA.Inst.GetByPrimaryKey(false, e.Item.Cells[0].Text);
            switch (e.CommandName)
            {
                case "Edit":
                    Insert = false;
                    txtMaLV.Text = itemLV.MaLoaiVe.ToString();
                    txtMaLV.ReadOnly = true;
                    txtTenLV.Text = itemLV.TenLoaiVe.ToString();
                    txtPhiHuy.Text = itemLV.PhiHuy.ToString();                      
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;

                case "Delete":
                    LoaiVeDA.Inst.Delete(itemLV);
                    LoadGrid();
                    break;
            }
        }

        protected void grdLoaiVe_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                var CCB = (LoaiVe)e.Item.DataItem;                
                var lbl = e.Item.FindControl("lblPhiHuy") as Label;
                lbl.Text = String.Format("{0:0,0}", CCB.PhiHuy);                
            }
        }

        protected void grdLoaiVe_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {

        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        

        
    }
}